# module geometry.twoD.polygon

import copy

import line
import polyline

class Polygon(polyline.Polyline):
	# Implements a polygon, given the points at the perimeter
	def __repr__(self):
		# Returns the python-representation for the object
		return 'Polygon(' + repr(self.vertices) + ',' + repr(self.width) + ')'
	def __str__(self):
		# Returns a printable string representation of the object
		return self.__repr__()
	def edges(self):
		rv = [ ]
		for i in range(len(self.vertices)-1):
			rv.append(line.Line(self.vertices[i],self.vertices[i+1],self.width))
		rv.append(line.Line(self.vertices[-1],self.vertices[0],self.width))
		return rv
	def isInArea(self,pt):
		# Returns true (1) if the point 'pt' lies within the polygon
		rv = 0
		x = pt.x
		y = pt.y
		j = len(self.vertices)-1
		for i in range(len(self.vertices)):
			xp_i, yp_i = self.vertices[i].x, self.vertices[i].y
			xp_j, yp_j = self.vertices[j].x, self.vertices[j].y
			if ( ((yp_i <= y) and (y < yp_j)) or \
			     ((yp_j <= y) and (y < yp_i)) ) and \
		    	 ( x < (xp_j - xp_i) * (y - yp_i) / (yp_j - yp_i) + xp_i ) :
				rv = not rv
		        j = i
		return rv
	#########################################################################################
	# Optional methods (default to no-action)
	#########################################################################################
	def draw(self,canvas=None):
		# Draws the object on the canvas (if provided)
		pass
	def erase(self,canvas=None):
		# Erases the object from the canvas (if provided)
		pass
	def select(self):
		# Selects the feature 
		pass
	def deselect(self):
		# Deselects the feature
		pass
	def click(self,event):
		# Callback (optional) for mouse clicks
		pass
	def doubleClick(self,event):
		# Callback (optional) for mouse double-clicks
		pass
	def rightClick(self,event):
		# Callback (optional) for mouse right-clicks
		pass
	def mouseDrag(self,event):
		# Callback (optional) for mouse drags
		pass
	def mouseMove(self,event):
		# Callback (optional) for mouse moves
		pass
